import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

/// 初始化图片请求地址，常用于设置cdn
void setAssetPrefix(String url) {}

Widget imageFromAsset(
  String asset, {
  Alignment alignment = Alignment.center,
  Color? color,
  bool excludeFromSemantics = false,
  BoxFit fit = BoxFit.contain,
  double? height,
  Key? key,
  String? package,
  WidgetBuilder? placeholderBuilder,
  String? semanticsLabel,
  double? width,
  ImageFrameBuilder? frameBuilder,
}) {
  return Image.asset(
    asset,
    alignment: alignment,
    color: color,
    excludeFromSemantics: excludeFromSemantics,
    fit: fit,
    height: height,
    key: key,
    package: package,
    width: width,
    frameBuilder: frameBuilder,
  );
}

Widget svgFromAsset(
  String asset, {
  Alignment alignment = Alignment.center,
  Color? color,
  bool excludeFromSemantics = false,
  BoxFit fit = BoxFit.contain,
  double? height,
  Key? key,
  String? package,
  WidgetBuilder? placeholderBuilder,
  String? semanticsLabel,
  double? width,
  ImageFrameBuilder? frameBuilder,
}) {
  return SvgPicture.asset(
    asset,
    alignment: alignment,
    color: color,
    fit: fit,
    height: height,
    package: package,
    width: width,
  );
}

Widget svgFromFile({
  required String imageUrl,
  required BoxFit fit,
  double? width,
  double? height,
  Color? color,
  WidgetBuilder? placeholderBuilder,
}) {
  return SvgPicture.file(
    File(imageUrl),
    width: width,
    height: height,
    color: color,
    fit: fit,
    placeholderBuilder: (context) {
      return placeholderBuilder!(context);
    },
  );
}

Widget imageFromFile(
  String imageUrl, {
  double? width,
  double? height,
  Color? color,
  BoxFit? fit,
  String? placeHoldUrl,
  ImageFrameBuilder? frameBuilder, //无用
  bool loadingAnimation = false,
  Widget placeHoldWidget = const SizedBox(),
}) {
  return Image.file(
    File(imageUrl.isNotEmpty ? imageUrl : placeHoldUrl ?? ''),
    fit: fit ?? BoxFit.cover,
    width: width,
    height: height,
    color: color,
    frameBuilder: loadingAnimation
        ? (context, child, frame, wasSynchronouslyLoaded) {
            if (wasSynchronouslyLoaded) {
              return child;
            }

            ///图片加载动画
            return loadingAnimation
                ? AnimatedSwitcher(
                    duration: const Duration(milliseconds: 500),
                    child: frame != null ? child : placeHoldWidget,
                  )
                : placeHoldWidget;
          }
        : null,
  );
}
